package tc.common.display;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.geom.Point2D;

import javax.swing.UIManager;

import tc.common.symbology.Style;
import tc.common.utility.TcDisplayTransf;
import tc.common.utility.TcDisplayXY;
import tc.crase.view.ViewRoot;

public class GentityText extends Gentity {
	String         text;
	Point2D.Double position;
	public boolean        selected = false;
	
	static public Font fontObject;
	static public int SHAPE_OFFSET = 10;
	
	static {
		fontObject = UIManager.getFont("Panel.font");
		fontObject = new Font(fontObject.getName(), Font.BOLD, fontObject.getSize());
	}
	
	public GentityText(String text, Point2D.Double position) {
		this.text     = text;
		this.position = position;
	}
	
	public void display(Graphics g, Point position) {
		Font oldFont = g.getFont();
		Color oldColor = g.getColor();
		g.setFont(GentityText.fontObject);
		
		Point2D.Double pt = new Point2D.Double((int)position.x, (int)position.y);
		GentityText.ObjectRectangle r = GentityText.getObjectRectangle(g, text, pt, style);
		
		if (selected) {
			int offset = SHAPE_OFFSET + (int)(style.solidOffset()*4) + 6;
			ViewRoot.styleSelected.drawRect( g, r.x, r.y, r.width, r.height, offset);
		}
		
		style.drawRect( g, r.x, r.y, r.width, r.height, SHAPE_OFFSET);

		g.setColor(oldColor);
		g.drawString( text, r.x, r.y + r.ascent);
		
		g.setFont(oldFont);
		g.setColor(oldColor);
	}
	
	@Override
	public void display(Graphics g, TcDisplayTransf transf) {
		TcDisplayXY XY = new TcDisplayXY(position, transf, TcDisplayXY.CoordSys.WORLD);
		XY.transform(TcDisplayXY.CoordSys.DEVICE);
		
		Point position = new Point((int)XY.xy.x, (int)XY.xy.y);
		
		display(g, position);
		
		/*Font oldFont = g.getFont();
		Color oldColor = g.getColor();
		g.setFont(GentityText.fontObject);
		
		GentityText.ObjectRectangle r = GentityText.getObjectRectangle(g, text, XY.xy, style);
		
		style.drawRect( g, r.x, r.y, r.width, r.height, SHAPE_OFFSET);

		g.setColor(oldColor);
		g.drawString( text, r.x, r.y + r.ascent);
		
		g.setFont(oldFont);
		g.setColor(oldColor);*/
	}
	
	public ObjectRectangle getObjectRectangle(Graphics g, Point2D.Double xy) {
		return getObjectRectangle(g, text, xy, style);
	}
	
	static public ObjectRectangle getObjectRectangle(Graphics g, String content, Point2D.Double xy, Style style) {
		if (content == null) {
			content = "TODO";
		}
		
		Font oldFont = g.getFont();
		g.setFont(GentityText.fontObject);
		FontMetrics fm = g.getFontMetrics();

		int height = fm.getHeight();
		int width = fm.stringWidth(content);

		int x = (int)xy.x - width / 2;
		int y = (int)xy.y - height / 2;
		
		g.setFont(oldFont);
		
		int offset = 0;
		if (style != null) {
			offset = (int)(style.shapeOffset()*4.0) + SHAPE_OFFSET;
		}
		
		return new GentityText.ObjectRectangle(x, y, width, height, fm.getAscent(), offset);
	}
	
	static public class ObjectRectangle {
		public int x;
		public int y;
		public int width;
		public int height;
		public int ascent;
		public int offset;
		
		public ObjectRectangle(int x, int y, int width, int height, int ascent, int offset) {
			this.x = x;
			this.y = y;
			this.width = width;
			this.height = height;
			this.ascent = ascent;
			this.offset = offset;
		}
	}	
}
